from flask import Blueprint,request,jsonify

import service.device.device_manager as device_manager
from service.task import task_service

app = Blueprint(__name__,__name__)

@app.route('/device/changeRecv',methods=["post"])
def changeRecv():
    """ 设置资料
    ---
    tags: ["device"]
    parameters:
      - name: deviceName
        in: formData
      - name: recv
        in: formData
    responses:
      200:
        description: ""
    """
    deviceName = request.form['deviceName']
    recv = int(request.form['recv'])
    device_manager.changeRecv(deviceName,recv)
    return jsonify({})
    
@app.route('/device/addDevice',methods=["post"])
def addDevice():
    """ 设置资料
    ---
    tags: ["device"]
    parameters:
      - name: deviceName
        in: formData
    responses:
      200:
        description: ""
    """
    deviceName = request.form['deviceName']
    device_manager.addDeviceActor(deviceName)
    return jsonify({})
    

@app.route('/device/findListDevice',methods=["get"])
def findListDevice():
    """ 设置资料
    ---
    tags: ["device"]
    parameters: []
    responses:
      200:
        description: ""
    """
    deviceActorList = device_manager.listDeviceActor()
    dlist = []
    for deviceActor in deviceActorList:
        taskNum = task_service.countTask(deviceActor.deviceName)
        dlist.append({
            "deviceName":deviceActor.deviceName,
            "state":deviceActor.state,
            "recv":deviceActor.recv,
            "taskNum":taskNum
        })
    return jsonify(dlist)
    

@app.route('/device/findListDeviceName',methods=["get"])
def findListDeviceName():
    """ 设置资料
    ---
    tags: ["device"]
    parameters: []
    responses:
      200:
        description: ""
    """
    deviceActorList = device_manager.listDeviceActor()
    dlist = []
    for deviceActor in deviceActorList:
        dlist.append(deviceActor.deviceName)
    return jsonify(dlist)
    
